package cn.parus.service.impl;

import cn.parus.common.Execption.CustomException;
import cn.parus.common.utils.UUidUtils;
import cn.parus.entity.User;
import cn.parus.entity.dto.UserLoginDTO;
import cn.parus.entity.dto.UserPasswordDTO;
import cn.parus.entity.dto.UserProfileDTO;
import cn.parus.entity.dto.UserRegisterDTO;
import cn.parus.entity.user.vo.UserProfileVO;
import cn.parus.mapper.UserMapper;
import cn.parus.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author: Axel
 * @Description: TODO
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 登录逻辑
     *
     * @param dto
     * @return
     */
    @Override
    @Scope("prototype") // 为保障线程安全，每次登录都要求创建新的实例
    public UserProfileVO login(UserLoginDTO dto) {
        // 根据用户名查询用户
        User user = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, dto.getUsername()));

        // 用户不存在
        if (user == null) {
            throw new CustomException("用户不存在");
        }

        // 密码校验
        if (!user.getPassword().equals(dto.getPassword())) {
            throw new CustomException("账号或密码错误");
        }

        // 将User对象赋给userProfileVo，增强安全性
        UserProfileVO userProfileVO = new UserProfileVO();
        BeanUtils.copyProperties(user, userProfileVO);
        return userProfileVO;
    }

    /**
     * 注册逻辑
     *
     * @param userRegisterDTO
     * @return
     */
    @Scope("prototype")
    @Override
    public User register(UserRegisterDTO userRegisterDTO) {
        // 检查用户名状态
        User existingUser = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, userRegisterDTO.getUsername()));

        // 如果用户名已经存在
        if (existingUser != null) {
            throw new CustomException("用户名已存在");
        }

        // 实例化新的存储
        User user = new User();
        user.setUsername(userRegisterDTO.getUsername());
        user.setPassword(userRegisterDTO.getPassword());
        user.setEmail(userRegisterDTO.getEmail());
        user.setStorageQuota(1073741824L);
        user.setUsedStorage(0L);

        // 保存到数据库中
        this.save(user);
        return user;
    }

    /**
     * 修改密码
     *
     * @param userPasswordDTO
     * @return
     */
    @Override
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        // 检查用户名状态
        User user = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, userPasswordDTO.getUsername()));
        if (user == null) {
            throw new CustomException("用户不存在");
        }

        // 验证旧密码
        if (!user.getPassword().equals(userPasswordDTO.getPassword())) {
            throw new CustomException("旧密码错误");
        }

        // 验证新密码和确认密码是否一致
        if (!userPasswordDTO.getNewPassword().equals(userPasswordDTO.getConfirmPassword())) {
            throw new CustomException("新密码和确认密码不一致");
        }

        // 更新密码
        user.setPassword(userPasswordDTO.getNewPassword());

        // 保存到数据库中
        this.updateById(user);
    }

    /**
     * 修改个人信息
     *
     * @param userProfileDTO
     * @return
     */
    @Override
    public void updateInfo(UserProfileDTO userProfileDTO) {
        // 根据用户名查询用户
        User user = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, userProfileDTO.getUsername()));

        if (user == null) {
            throw new CustomException("用户不存在");
        }

        // 检查并更新字段
        Optional.ofNullable(userProfileDTO.getEmail()).ifPresent(user::setEmail);
        Optional.ofNullable(userProfileDTO.getPhone()).ifPresent(user::setPhone);
        Optional.ofNullable(userProfileDTO.getStorageQuota()).ifPresent(user::setStorageQuota);
        Optional.ofNullable(userProfileDTO.getUsedStorage()).ifPresent(user::setUsedStorage);
        Optional.ofNullable(userProfileDTO.getAvatar()).ifPresent(user::setAvatar);
        Optional.ofNullable(userProfileDTO.getRealName()).ifPresent(user::setRealName);

        // 保存修改到数据库
        this.updateById(user);
    }
}
